package com.it.flowable.service.leave.bpm;

import com.alibaba.fastjson.JSON;
import com.it.flowable.common.constants.BpmLeaveConstant;
import com.it.flowable.entity.leave.ActFlwServiceDataLeaveVO;
import com.it.flowable.mapper.leave.ActFlwServiceDataLeaveVOMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.TaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BpmLeaveTaskService {

    @Resource
    private ProcessEngine processEngine;
    @Resource
    private BpmLeaveDao bpmLeaveDao;
    @Resource
    private ActFlwServiceDataLeaveVOMapper actFlwServiceDataLeaveVOMapper;

    public void acceptTask(String taskId, String userId) throws Exception {
        TaskService taskService = processEngine.getTaskService();
        if (StringUtils.isEmpty(taskId)) {
            throw new Exception("接收任务失败：BpmLeaveTaskService.acceptTask，taskId任务Id为空，请联系管理员");
        }
        if (StringUtils.isEmpty(userId)) {
            throw new Exception("接收任务失败：BpmLeaveTaskService.acceptTask，userId接收操作员Id为空，请联系管理员");
        }

        //(1)工作流接收任务
        if (taskService.createTaskQuery().taskId(taskId).taskUnassigned() != null) {
            taskService.claim(taskId, userId);
        }
        //(2)业务_任务扩展表更新
        bpmLeaveDao.updateTaskExtByAccept(taskId, userId);
    }

    public List<Map<String, Object>> completeTask(String taskId, Map<String, Object> bpmParamMap) throws Exception {
        TaskService taskService = processEngine.getTaskService();
        //(1)清空线程变量
        BpmLeaveThreadLocalRe.clear();
        BpmLeaveThreadLocalRe.bindData();
        //(2-1)车理赔业务_任务是否已完成校验
        checkTaskExtByComplete(taskId);
        //(2-2)工作流_完成人工任务
        log.info("工作流中completeTask输出bpmParamMap数据：" + JSON.toJSONString(bpmParamMap) + " taskId" + taskId);
        taskService.complete(taskId, bpmParamMap);
        //(2-3)处理要返回给业务系统的待办任务信息
//        return WorkflowClmUtil.packageCompletRMsg(bpmParamMap);
        return BpmLeaveThreadLocalRe.getSessionData(BpmLeaveThreadLocalRe.getThreadLocalDataAll());
    }

    /**
     * 校验任务扩展表任务状态是否可以完成任务
     *
     * @param taskId
     * @return ActClmTaskExtVO 说明
     * @throws Exception 说明
     */
    //**************************************************************************
    public void checkTaskExtByComplete(String taskId) throws Exception {
        if (StringUtils.isEmpty(taskId)) {
            throw new Exception("通过taskId校验任务扩展表任务状态是否可以完成任务失败：BpmLeaveTaskService.checkTaskExtByComplete，taskId任务ID为空，请联系管理员");
        }
        ActFlwServiceDataLeaveVO task = actFlwServiceDataLeaveVOMapper.selectById(taskId);
        String cStatus = task.getCStatus();
        List<String> canCompleteStates = Arrays.asList(BpmLeaveConstant.LEAVE_STATUS_INIT, BpmLeaveConstant.LEAVE_STATUS_ACCEPT,
                BpmLeaveConstant.LEAVE_STATUS_TEMP);
        if (canCompleteStates.contains(cStatus)) {
            // 放行
        } else if (BpmLeaveConstant.LEAVE_STATUS_COMPLETE.equals(cStatus)) {
            throw new Exception("该任务已被" + task.getCOwnerId() + "处理完成！");
        } else if (BpmLeaveConstant.LEAVE_STATUS_CANCEL.equals(cStatus)) {
            throw new Exception("该任务已被注销,请确认！");
        } else if (BpmLeaveConstant.LEAVE_STATUS_HANG_UP.equals(cStatus)) {
            throw new Exception("该任务已被挂起,请确认！");
        }
    }

}
